/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.impl;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.mina.common.IoSession;

import com.cqcis.uip.base.framework.common.HttpClient;
import com.cqcis.uip.base.framework.common.Task;
import com.cqcis.uip.base.framework.core.RegisteredServiceContext;

/**
 * HTTP 客户端task上下文实现
 * 
 * @author huangmy
 * @date   2008-10-13
 */
public abstract class AbstractHttpClientTaskContext extends AbstractTaskContext<Object> {
    public static String HTTPCLIENT = "HTTPCLIENT_" + AbstractHttpClientTaskContext.class.getName();
	
	public AbstractHttpClientTaskContext(RegisteredServiceContext serviceContext, Task task) {
		super(serviceContext, task);	
	}
	
	@Override
	public Object doBeforeWrite(Object o) {		
		return o;
	}

	public Object read() throws Exception {
		return super.doRead();
	}
	
	@Override
	protected void checkOperationValid() {
		// don't check
	}
	
	@Override
	protected void doWrite(Object o) throws Exception {
		doHttpClientWrite(getTask(), (HttpMethod)o); // 同步阻塞写
	}
	
	@Override
	protected IoSession getSessionForTask() {
		return null; // 返回null
	}
		
	/**
	 * 写
	 * 
	 * @param task
	 * @param method
	 * @throws Exception
	 */
	protected abstract void doHttpClientWrite(Task task, HttpMethod method) throws Exception;
	
	/**
	 * HTTP CLIENT
	 * 
	 * @return
	 */
	public HttpClient getHttpClient() {
		return (HttpClient)getTask().getAttribute(HTTPCLIENT);
	}
}
